﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;

namespace RemotePrintTimer
{
    public class EncryptHelper
    {
        #region 加解密

        const string Key = "1234567890adbcde";
        const string IV = "1234567890hjlkew";

        public static string Encrypt(string originStr, string key = Key)
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    //aes的key和iv有长度要求
                    aes.Key = Encoding.Unicode.GetBytes(key).Take(16).ToArray();
                    aes.IV = Encoding.Unicode.GetBytes(IV).Take(16).ToArray();
                    var enc = aes.CreateEncryptor(aes.Key, aes.IV);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write))
                        {
                            var bts = Encoding.Unicode.GetBytes(originStr);
                            cs.Write(bts, 0, bts.Length);
                        }
                        //用Base64编码存储加密后的字符串；其他编码格式可能因不可显示字符而造成数据丢失
                        return Convert.ToBase64String(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {


            }
            return originStr;
        }

        public static string Decrypt(string encryptedStr, string key = Key)
        {
            try
            {
                using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
                {
                    aes.Key = Encoding.Unicode.GetBytes(key).Take(32).ToArray();
                    aes.IV = Encoding.Unicode.GetBytes(IV).Take(16).ToArray();
                    var enc = aes.CreateDecryptor(aes.Key, aes.IV);
                    using (MemoryStream ms = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ms, enc, CryptoStreamMode.Write))
                        {
                            var bts = Convert.FromBase64String(encryptedStr);
                            cs.Write(bts, 0, bts.Length);
                        }
                        return Encoding.Unicode.GetString(ms.ToArray());
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return encryptedStr;
        }
        #endregion

        public static string Encrypt2(string word, string keyStr, string ivStr)
        {
            var key = Encoding.UTF8.GetBytes(keyStr);
            var iv = Encoding.UTF8.GetBytes(ivStr);

            var srcs = Encoding.UTF8.GetBytes(word);

            using (var aes = Aes.Create())
            {
                aes.Key = key;
                aes.IV = iv;
                aes.Mode = CipherMode.CBC;
                aes.Padding = PaddingMode.Zeros;

                using (var encryptor = aes.CreateEncryptor())
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(srcs, 0, srcs.Length);
                            cryptoStream.FlushFinalBlock();

                            var encryptedBytes = memoryStream.ToArray();
                            return Convert.ToBase64String(encryptedBytes);
                        }
                    }
                }
            }
        }

        public static string Decrypt2(string data, string key, string iv)
        {
            try
            {
                byte[] encryptedData = Convert.FromBase64String(data);

                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Key = Encoding.UTF8.GetBytes(key);
                    aesAlg.IV = Encoding.UTF8.GetBytes(iv);
                    aesAlg.Mode = CipherMode.CBC;
                    aesAlg.Padding = PaddingMode.Zeros;

                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

                    using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                return srDecrypt.ReadToEnd().Replace("\0", "");
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return null;
            }
        }

    }
}
